﻿using System;
using System.Net;
using System.Collections;
using System.Threading;
using ClientEndpoint;

namespace TestingClient
{
	class Program
	{
		static int Main(string[] args)
		{
			try
			{
			IPAddress address;
			if (args.Length != 2)
			{
				Console.WriteLine("arguments error");
				return 1;
			}
			int port;
			if (!Int32.TryParse(args[0], out port))
			{
				Console.WriteLine("parsing first argument error");
				return 2;
			}
			try
			{
				address = IPAddress.Parse(args[1]);
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
				return 3;
			}
			Console.WriteLine(address);
			var ports = new ArrayList {20, 10, 4};
				var name = Console.ReadLine();
			ClientProperties.getInstance(name, ports, Package.HelloPackage.NodeType.UNI);
			var cCend = CableCloudEndPoint.getInstance(address, port, CableCloudEndPoint.debugHandler);
			var receiving = new Thread(cCend.startListeningForCells) {IsBackground = true};
			receiving.Start();
            var sending = new Thread(sendingHandler);
            sending.Start();
            sending.Join();
			}
			catch (WrongNodeNameException e)
			{
				Console.WriteLine(e.ToString());
			}
			catch (WrongCodeException e)
			{
				Console.WriteLine(e.ToString());
			}
			return 0;
		}
        static void sendingHandler()
        {
	        var sender = Sender.getInstance();
            while (true)
            {
                Console.WriteLine("Port:");
                var port = Int32.Parse(Console.ReadLine());
                Console.WriteLine("VPI:");
                var vpi = Int32.Parse(Console.ReadLine());
                Console.WriteLine("VCI:");
                var vci = Int32.Parse(Console.ReadLine());
                Console.WriteLine("Message:");
                var message = Console.ReadLine();
                sender.sendMessage(message, port, vpi, vci);  
            }
        }
	}
}